<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Socket - networking constants and support functions - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/Socket">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Socket.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/Socket">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/Socket">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/Socket">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/Socket">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/Socket">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/Socket">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/Socket">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/Socket">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/Socket">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/Socket">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/Socket">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/Socket">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/Socket">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/Socket">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/Socket">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/Socket">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/Socket">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/Socket">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/Socket">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/Socket">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/Socket">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/Socket">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/Socket">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/Socket">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/Socket">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/Socket">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/Socket">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/Socket">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/Socket">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/Socket">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/Socket">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/Socket">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/Socket">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/Socket">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/Socket">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/Socket">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/Socket">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/Socket">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/Socket">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/Socket">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/Socket">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/Socket">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/Socket">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/Socket">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/Socket">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/Socket">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/Socket">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/Socket">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/Socket">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/Socket">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/Socket">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/Socket">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/Socket">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/Socket">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/Socket">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/Socket">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/Socket">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/Socket">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/Socket">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/Socket">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/Socket">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/Socket">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/Socket">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/Socket">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/Socket">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/Socket">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/Socket">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/Socket">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/Socket">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/Socket">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/Socket">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/Socket">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/Socket">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/Socket">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/Socket">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/Socket">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/Socket">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/Socket">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/Socket">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/Socket">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Socket.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/Socket">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/Socket">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/Socket">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/Socket">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/Socket">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/Socket">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/Socket">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/Socket">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/Socket">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/Socket">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/Socket">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/Socket">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/Socket">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/Socket">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/Socket">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/Socket">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/Socket">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/Socket">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/Socket">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/Socket">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/Socket">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/Socket">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/Socket">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/Socket">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/Socket">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/Socket">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/Socket">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/Socket">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/Socket">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/Socket">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/Socket">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/Socket">5.35.1</a>
          <a class="dropdown-item active" href="Socket.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Socket.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="Socket.html">Socket</a>
          <div id="more">
            (<a href="Socket.txt">source</a>,
            <a href="https://metacpan.org/pod/Socket">CPAN</a>)
          </div>
            <div id="moduleversion">version 2.031</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <div class="leading-notice">
          <strong>Socket</strong> may also refer to the function: <a href="functions/socket.html">socket</a>
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="Socket.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#SYNOPSIS">SYNOPSIS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#DESCRIPTION">DESCRIPTION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#CONSTANTS">CONSTANTS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Socket.html#PF_INET,-PF_INET6,-PF_UNIX,-...">PF_INET, PF_INET6, PF_UNIX, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#AF_INET,-AF_INET6,-AF_UNIX,-...">AF_INET, AF_INET6, AF_UNIX, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#SOCK_STREAM,-SOCK_DGRAM,-SOCK_RAW,-...">SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#SOCK_NONBLOCK.-SOCK_CLOEXEC">SOCK_NONBLOCK. SOCK_CLOEXEC</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#SOL_SOCKET">SOL_SOCKET</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#SO_ACCEPTCONN,-SO_BROADCAST,-SO_ERROR,-...">SO_ACCEPTCONN, SO_BROADCAST, SO_ERROR, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#IP_OPTIONS,-IP_TOS,-IP_TTL,-...">IP_OPTIONS, IP_TOS, IP_TTL, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#IP_PMTUDISC_WANT,-IP_PMTUDISC_DONT,-...">IP_PMTUDISC_WANT, IP_PMTUDISC_DONT, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#IPTOS_LOWDELAY,-IPTOS_THROUGHPUT,-IPTOS_RELIABILITY,-...">IPTOS_LOWDELAY, IPTOS_THROUGHPUT, IPTOS_RELIABILITY, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#MSG_BCAST,-MSG_OOB,-MSG_TRUNC,-...">MSG_BCAST, MSG_OOB, MSG_TRUNC, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#SHUT_RD,-SHUT_RDWR,-SHUT_WR">SHUT_RD, SHUT_RDWR, SHUT_WR</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#INADDR_ANY,-INADDR_BROADCAST,-INADDR_LOOPBACK,-INADDR_NONE">INADDR_ANY, INADDR_BROADCAST, INADDR_LOOPBACK, INADDR_NONE</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#IPPROTO_IP,-IPPROTO_IPV6,-IPPROTO_TCP,-...">IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#TCP_CORK,-TCP_KEEPALIVE,-TCP_NODELAY,-...">TCP_CORK, TCP_KEEPALIVE, TCP_NODELAY, ...</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#IN6ADDR_ANY,-IN6ADDR_LOOPBACK">IN6ADDR_ANY, IN6ADDR_LOOPBACK</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#IPV6_ADD_MEMBERSHIP,-IPV6_MTU,-IPV6_V6ONLY,-...">IPV6_ADD_MEMBERSHIP, IPV6_MTU, IPV6_V6ONLY, ...</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#STRUCTURE-MANIPULATORS">STRUCTURE MANIPULATORS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Socket.html#$family-=-sockaddr_family-$sockaddr">$family = sockaddr_family $sockaddr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$sockaddr-=-pack_sockaddr_in-$port,-$ip_address">$sockaddr = pack_sockaddr_in $port, $ip_address</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($port,-$ip_address)-=-unpack_sockaddr_in-$sockaddr">($port, $ip_address) = unpack_sockaddr_in $sockaddr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$sockaddr-=-sockaddr_in-$port,-$ip_address">$sockaddr = sockaddr_in $port, $ip_address</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($port,-$ip_address)-=-sockaddr_in-$sockaddr">($port, $ip_address) = sockaddr_in $sockaddr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$sockaddr-=-pack_sockaddr_in6-$port,-$ip6_address,-%5B$scope_id,-%5B$flowinfo%5D%5D">$sockaddr = pack_sockaddr_in6 $port, $ip6_address, [$scope_id, [$flowinfo]]</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($port,-$ip6_address,-$scope_id,-$flowinfo)-=-unpack_sockaddr_in6-$sockaddr">($port, $ip6_address, $scope_id, $flowinfo) = unpack_sockaddr_in6 $sockaddr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$sockaddr-=-sockaddr_in6-$port,-$ip6_address,-%5B$scope_id,-%5B$flowinfo%5D%5D">$sockaddr = sockaddr_in6 $port, $ip6_address, [$scope_id, [$flowinfo]]</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($port,-$ip6_address,-$scope_id,-$flowinfo)-=-sockaddr_in6-$sockaddr">($port, $ip6_address, $scope_id, $flowinfo) = sockaddr_in6 $sockaddr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$sockaddr-=-pack_sockaddr_un-$path">$sockaddr = pack_sockaddr_un $path</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($path)-=-unpack_sockaddr_un-$sockaddr">($path) = unpack_sockaddr_un $sockaddr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$sockaddr-=-sockaddr_un-$path">$sockaddr = sockaddr_un $path</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($path)-=-sockaddr_un-$sockaddr">($path) = sockaddr_un $sockaddr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$ip_mreq-=-pack_ip_mreq-$multiaddr,-$interface">$ip_mreq = pack_ip_mreq $multiaddr, $interface</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($multiaddr,-$interface)-=-unpack_ip_mreq-$ip_mreq">($multiaddr, $interface) = unpack_ip_mreq $ip_mreq</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$ip_mreq_source-=-pack_ip_mreq_source-$multiaddr,-$source,-$interface">$ip_mreq_source = pack_ip_mreq_source $multiaddr, $source, $interface</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($multiaddr,-$source,-$interface)-=-unpack_ip_mreq_source-$ip_mreq">($multiaddr, $source, $interface) = unpack_ip_mreq_source $ip_mreq</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$ipv6_mreq-=-pack_ipv6_mreq-$multiaddr6,-$ifindex">$ipv6_mreq = pack_ipv6_mreq $multiaddr6, $ifindex</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($multiaddr6,-$ifindex)-=-unpack_ipv6_mreq-$ipv6_mreq">($multiaddr6, $ifindex) = unpack_ipv6_mreq $ipv6_mreq</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#FUNCTIONS">FUNCTIONS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Socket.html#$ip_address-=-inet_aton-$string">$ip_address = inet_aton $string</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$string-=-inet_ntoa-$ip_address">$string = inet_ntoa $ip_address</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$address-=-inet_pton-$family,-$string">$address = inet_pton $family, $string</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#$string-=-inet_ntop-$family,-$address">$string = inet_ntop $family, $address</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($err,-@result)-=-getaddrinfo-$host,-$service,-%5B$hints%5D">($err, @result) = getaddrinfo $host, $service, [$hints]</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#($err,-$hostname,-$servicename)-=-getnameinfo-$sockaddr,-%5B$flags,-%5B$xflags%5D%5D">($err, $hostname, $servicename) = getnameinfo $sockaddr, [$flags, [$xflags]]</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#getaddrinfo()-/-getnameinfo()-ERROR-CONSTANTS">getaddrinfo() / getnameinfo() ERROR CONSTANTS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#EXAMPLES">EXAMPLES</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Socket.html#Lookup-for-connect()">Lookup for connect()</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#Making-a-human-readable-string-out-of-an-address">Making a human-readable string out of an address</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#Resolving-hostnames-into-IP-addresses">Resolving hostnames into IP addresses</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#Accessing-socket-options">Accessing socket options</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Socket.html#AUTHOR">AUTHOR</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="Socket.html#NAME">#</a>NAME</h1>

<p><code>Socket</code> - networking constants and support functions</p>

<h1 id="SYNOPSIS"><a class="permalink" href="Socket.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<p><code>Socket</code> a low-level module used by, among other things, the <a href="./IO::Socket.html">IO::Socket</a> family of modules. The following examples demonstrate some low-level uses but a practical program would likely use the higher-level API provided by <code>IO::Socket</code> or similar instead.</p>

<pre><code>use Socket qw(PF_INET SOCK_STREAM pack_sockaddr_in inet_aton);

socket(my $socket, PF_INET, SOCK_STREAM, 0)
    or die &quot;socket: $!&quot;;

my $port = getservbyname &quot;echo&quot;, &quot;tcp&quot;;
connect($socket, pack_sockaddr_in($port, inet_aton(&quot;localhost&quot;)))
    or die &quot;connect: $!&quot;;

print $socket &quot;Hello, world!\n&quot;;
print &lt;$socket&gt;;</code></pre>

<p>See also the <a href="Socket.html#EXAMPLES">&quot;EXAMPLES&quot;</a> section.</p>

<h1 id="DESCRIPTION"><a class="permalink" href="Socket.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>This module provides a variety of constants, structure manipulators and other functions related to socket-based networking. The values and functions provided are useful when used in conjunction with Perl core functions such as socket(), setsockopt() and bind(). It also provides several other support functions, mostly for dealing with conversions of network addresses between human-readable and native binary forms, and for hostname resolver operations.</p>

<p>Some constants and functions are exported by default by this module; but for backward-compatibility any recently-added symbols are not exported by default and must be requested explicitly. When an import list is provided to the <code>use Socket</code> line, the default exports are not automatically imported. It is therefore best practice to always to explicitly list all the symbols required.</p>

<p>Also, some common socket &quot;newline&quot; constants are provided: the constants <code>CR</code>, <code>LF</code>, and <code>CRLF</code>, as well as <code>$CR</code>, <code>$LF</code>, and <code>$CRLF</code>, which map to <code>\015</code>, <code>\012</code>, and <code>\015\012</code>. If you do not want to use the literal characters in your programs, then use the constants provided here. They are not exported by default, but can be imported individually, and with the <code>:crlf</code> export tag:</p>

<pre><code>use Socket qw(:DEFAULT :crlf);

$sock-&gt;print(&quot;GET / HTTP/1.0$CRLF&quot;);</code></pre>

<p>The entire getaddrinfo() subsystem can be exported using the tag <code>:addrinfo</code>; this exports the getaddrinfo() and getnameinfo() functions, and all the <code>AI_*</code>, <code>NI_*</code>, <code>NIx_*</code> and <code>EAI_*</code> constants.</p>

<h1 id="CONSTANTS"><a class="permalink" href="Socket.html#CONSTANTS">#</a>CONSTANTS</h1>

<p>In each of the following groups, there may be many more constants provided than just the ones given as examples in the section heading. If the heading ends <code>...</code> then this means there are likely more; the exact constants provided will depend on the OS and headers found at compile-time.</p>

<h2 id="PF_INET,-PF_INET6,-PF_UNIX,-..."><a class="permalink" href="Socket.html#PF_INET,-PF_INET6,-PF_UNIX,-...">#</a><a id="PF_INET"></a><a id="PF_INET-PF_INET6-PF_UNIX"></a>PF_INET, PF_INET6, PF_UNIX, ...</h2>

<p>Protocol family constants to use as the first argument to socket() or the value of the <code>SO_DOMAIN</code> or <code>SO_FAMILY</code> socket option.</p>

<h2 id="AF_INET,-AF_INET6,-AF_UNIX,-..."><a class="permalink" href="Socket.html#AF_INET,-AF_INET6,-AF_UNIX,-...">#</a><a id="AF_INET"></a><a id="AF_INET-AF_INET6-AF_UNIX"></a>AF_INET, AF_INET6, AF_UNIX, ...</h2>

<p>Address family constants used by the socket address structures, to pass to such functions as inet_pton() or getaddrinfo(), or are returned by such functions as sockaddr_family().</p>

<h2 id="SOCK_STREAM,-SOCK_DGRAM,-SOCK_RAW,-..."><a class="permalink" href="Socket.html#SOCK_STREAM,-SOCK_DGRAM,-SOCK_RAW,-...">#</a><a id="SOCK_STREAM"></a><a id="SOCK_STREAM-SOCK_DGRAM-SOCK_RAW"></a>SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, ...</h2>

<p>Socket type constants to use as the second argument to socket(), or the value of the <code>SO_TYPE</code> socket option.</p>

<h2 id="SOCK_NONBLOCK.-SOCK_CLOEXEC"><a class="permalink" href="Socket.html#SOCK_NONBLOCK.-SOCK_CLOEXEC">#</a><a id="SOCK_NONBLOCK"></a>SOCK_NONBLOCK. SOCK_CLOEXEC</h2>

<p>Linux-specific shortcuts to specify the <code>O_NONBLOCK</code> and <code>FD_CLOEXEC</code> flags during a <code>socket(2)</code> call.</p>

<pre><code>socket( my $sockh, PF_INET, SOCK_DGRAM|SOCK_NONBLOCK, 0 )</code></pre>

<h2 id="SOL_SOCKET"><a class="permalink" href="Socket.html#SOL_SOCKET">#</a>SOL_SOCKET</h2>

<p>Socket option level constant for setsockopt() and getsockopt().</p>

<h2 id="SO_ACCEPTCONN,-SO_BROADCAST,-SO_ERROR,-..."><a class="permalink" href="Socket.html#SO_ACCEPTCONN,-SO_BROADCAST,-SO_ERROR,-...">#</a><a id="SO_ACCEPTCONN"></a><a id="SO_ACCEPTCONN-SO_BROADCAST-SO_ERROR"></a>SO_ACCEPTCONN, SO_BROADCAST, SO_ERROR, ...</h2>

<p>Socket option name constants for setsockopt() and getsockopt() at the <code>SOL_SOCKET</code> level.</p>

<h2 id="IP_OPTIONS,-IP_TOS,-IP_TTL,-..."><a class="permalink" href="Socket.html#IP_OPTIONS,-IP_TOS,-IP_TTL,-...">#</a><a id="IP_OPTIONS"></a><a id="IP_OPTIONS-IP_TOS-IP_TTL"></a>IP_OPTIONS, IP_TOS, IP_TTL, ...</h2>

<p>Socket option name constants for IPv4 socket options at the <code>IPPROTO_IP</code> level.</p>

<h2 id="IP_PMTUDISC_WANT,-IP_PMTUDISC_DONT,-..."><a class="permalink" href="Socket.html#IP_PMTUDISC_WANT,-IP_PMTUDISC_DONT,-...">#</a><a id="IP_PMTUDISC_WANT"></a><a id="IP_PMTUDISC_WANT-IP_PMTUDISC_DONT"></a>IP_PMTUDISC_WANT, IP_PMTUDISC_DONT, ...</h2>

<p>Socket option value constants for <code>IP_MTU_DISCOVER</code> socket option.</p>

<h2 id="IPTOS_LOWDELAY,-IPTOS_THROUGHPUT,-IPTOS_RELIABILITY,-..."><a class="permalink" href="Socket.html#IPTOS_LOWDELAY,-IPTOS_THROUGHPUT,-IPTOS_RELIABILITY,-...">#</a><a id="IPTOS_LOWDELAY"></a><a id="IPTOS_LOWDELAY-IPTOS_THROUGHPUT-IPTOS_RELIABILITY"></a>IPTOS_LOWDELAY, IPTOS_THROUGHPUT, IPTOS_RELIABILITY, ...</h2>

<p>Socket option value constants for <code>IP_TOS</code> socket option.</p>

<h2 id="MSG_BCAST,-MSG_OOB,-MSG_TRUNC,-..."><a class="permalink" href="Socket.html#MSG_BCAST,-MSG_OOB,-MSG_TRUNC,-...">#</a><a id="MSG_BCAST"></a><a id="MSG_BCAST-MSG_OOB-MSG_TRUNC"></a>MSG_BCAST, MSG_OOB, MSG_TRUNC, ...</h2>

<p>Message flag constants for send() and recv().</p>

<h2 id="SHUT_RD,-SHUT_RDWR,-SHUT_WR"><a class="permalink" href="Socket.html#SHUT_RD,-SHUT_RDWR,-SHUT_WR">#</a><a id="SHUT_RD"></a><a id="SHUT_RD-SHUT_RDWR-SHUT_WR"></a>SHUT_RD, SHUT_RDWR, SHUT_WR</h2>

<p>Direction constants for shutdown().</p>

<h2 id="INADDR_ANY,-INADDR_BROADCAST,-INADDR_LOOPBACK,-INADDR_NONE"><a class="permalink" href="Socket.html#INADDR_ANY,-INADDR_BROADCAST,-INADDR_LOOPBACK,-INADDR_NONE">#</a><a id="INADDR_ANY"></a><a id="INADDR_ANY-INADDR_BROADCAST-INADDR_LOOPBACK-INADDR_NONE"></a>INADDR_ANY, INADDR_BROADCAST, INADDR_LOOPBACK, INADDR_NONE</h2>

<p>Constants giving the special <code>AF_INET</code> addresses for wildcard, broadcast, local loopback, and invalid addresses.</p>

<p>Normally equivalent to inet_aton(&#39;0.0.0.0&#39;), inet_aton(&#39;255.255.255.255&#39;), inet_aton(&#39;localhost&#39;) and inet_aton(&#39;255.255.255.255&#39;) respectively.</p>

<h2 id="IPPROTO_IP,-IPPROTO_IPV6,-IPPROTO_TCP,-..."><a class="permalink" href="Socket.html#IPPROTO_IP,-IPPROTO_IPV6,-IPPROTO_TCP,-...">#</a><a id="IPPROTO_IP"></a><a id="IPPROTO_IP-IPPROTO_IPV6-IPPROTO_TCP"></a>IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP, ...</h2>

<p>IP protocol constants to use as the third argument to socket(), the level argument to getsockopt() or setsockopt(), or the value of the <code>SO_PROTOCOL</code> socket option.</p>

<h2 id="TCP_CORK,-TCP_KEEPALIVE,-TCP_NODELAY,-..."><a class="permalink" href="Socket.html#TCP_CORK,-TCP_KEEPALIVE,-TCP_NODELAY,-...">#</a><a id="TCP_CORK"></a><a id="TCP_CORK-TCP_KEEPALIVE-TCP_NODELAY"></a>TCP_CORK, TCP_KEEPALIVE, TCP_NODELAY, ...</h2>

<p>Socket option name constants for TCP socket options at the <code>IPPROTO_TCP</code> level.</p>

<h2 id="IN6ADDR_ANY,-IN6ADDR_LOOPBACK"><a class="permalink" href="Socket.html#IN6ADDR_ANY,-IN6ADDR_LOOPBACK">#</a><a id="IN6ADDR_ANY"></a><a id="IN6ADDR_ANY-IN6ADDR_LOOPBACK"></a>IN6ADDR_ANY, IN6ADDR_LOOPBACK</h2>

<p>Constants giving the special <code>AF_INET6</code> addresses for wildcard and local loopback.</p>

<p>Normally equivalent to inet_pton(AF_INET6, &quot;::&quot;) and inet_pton(AF_INET6, &quot;::1&quot;) respectively.</p>

<h2 id="IPV6_ADD_MEMBERSHIP,-IPV6_MTU,-IPV6_V6ONLY,-..."><a class="permalink" href="Socket.html#IPV6_ADD_MEMBERSHIP,-IPV6_MTU,-IPV6_V6ONLY,-...">#</a><a id="IPV6_ADD_MEMBERSHIP"></a><a id="IPV6_ADD_MEMBERSHIP-IPV6_MTU-IPV6_V6ONLY"></a>IPV6_ADD_MEMBERSHIP, IPV6_MTU, IPV6_V6ONLY, ...</h2>

<p>Socket option name constants for IPv6 socket options at the <code>IPPROTO_IPV6</code> level.</p>

<h1 id="STRUCTURE-MANIPULATORS"><a class="permalink" href="Socket.html#STRUCTURE-MANIPULATORS">#</a><a id="STRUCTURE"></a>STRUCTURE MANIPULATORS</h1>

<p>The following functions convert between lists of Perl values and packed binary strings representing structures.</p>

<h2 id="$family-=-sockaddr_family-$sockaddr"><a class="permalink" href="Socket.html#$family-=-sockaddr_family-$sockaddr">#</a><a id="family-sockaddr_family-sockaddr"></a>$family = sockaddr_family $sockaddr</h2>

<p>Takes a packed socket address (as returned by pack_sockaddr_in(), pack_sockaddr_un() or the perl builtin functions getsockname() and getpeername()). Returns the address family tag. This will be one of the <code>AF_*</code> constants, such as <code>AF_INET</code> for a <code>sockaddr_in</code> addresses or <code>AF_UNIX</code> for a <code>sockaddr_un</code>. It can be used to figure out what unpack to use for a sockaddr of unknown type.</p>

<h2 id="$sockaddr-=-pack_sockaddr_in-$port,-$ip_address"><a class="permalink" href="Socket.html#$sockaddr-=-pack_sockaddr_in-$port,-$ip_address">#</a><a id="sockaddr-pack_sockaddr_in-port-ip_address"></a>$sockaddr = pack_sockaddr_in $port, $ip_address</h2>

<p>Takes two arguments, a port number and an opaque string (as returned by inet_aton(), or a v-string). Returns the <code>sockaddr_in</code> structure with those arguments packed in and <code>AF_INET</code> filled in. For Internet domain sockets, this structure is normally what you need for the arguments in bind(), connect(), and send().</p>

<p>An undefined $port argument is taken as zero; an undefined $ip_address is considered a fatal error.</p>

<h2 id="($port,-$ip_address)-=-unpack_sockaddr_in-$sockaddr"><a class="permalink" href="Socket.html#($port,-$ip_address)-=-unpack_sockaddr_in-$sockaddr">#</a><a id="port-ip_address-unpack_sockaddr_in-sockaddr"></a>($port, $ip_address) = unpack_sockaddr_in $sockaddr</h2>

<p>Takes a <code>sockaddr_in</code> structure (as returned by pack_sockaddr_in(), getpeername() or recv()). Returns a list of two elements: the port and an opaque string representing the IP address (you can use inet_ntoa() to convert the address to the four-dotted numeric format). Will croak if the structure does not represent an <code>AF_INET</code> address.</p>

<p>In scalar context will return just the IP address.</p>

<h2 id="$sockaddr-=-sockaddr_in-$port,-$ip_address"><a class="permalink" href="Socket.html#$sockaddr-=-sockaddr_in-$port,-$ip_address">#</a><a id="sockaddr-sockaddr_in-port-ip_address"></a>$sockaddr = sockaddr_in $port, $ip_address</h2>

<h2 id="($port,-$ip_address)-=-sockaddr_in-$sockaddr"><a class="permalink" href="Socket.html#($port,-$ip_address)-=-sockaddr_in-$sockaddr">#</a><a id="port-ip_address-sockaddr_in-sockaddr"></a>($port, $ip_address) = sockaddr_in $sockaddr</h2>

<p>A wrapper of pack_sockaddr_in() or unpack_sockaddr_in(). In list context, unpacks its argument and returns a list consisting of the port and IP address. In scalar context, packs its port and IP address arguments as a <code>sockaddr_in</code> and returns it.</p>

<p>Provided largely for legacy compatibility; it is better to use pack_sockaddr_in() or unpack_sockaddr_in() explicitly.</p>

<h2 id="$sockaddr-=-pack_sockaddr_in6-$port,-$ip6_address,-[$scope_id,-[$flowinfo]]"><a class="permalink" href="Socket.html#$sockaddr-=-pack_sockaddr_in6-$port,-$ip6_address,-%5B$scope_id,-%5B$flowinfo%5D%5D">#</a><a id="sockaddr-pack_sockaddr_in6-port-ip6_address-scope_id-flowinfo"></a>$sockaddr = pack_sockaddr_in6 $port, $ip6_address, [$scope_id, [$flowinfo]]</h2>

<p>Takes two to four arguments, a port number, an opaque string (as returned by inet_pton()), optionally a scope ID number, and optionally a flow label number. Returns the <code>sockaddr_in6</code> structure with those arguments packed in and <code>AF_INET6</code> filled in. IPv6 equivalent of pack_sockaddr_in().</p>

<p>An undefined $port argument is taken as zero; an undefined $ip6_address is considered a fatal error.</p>

<h2 id="($port,-$ip6_address,-$scope_id,-$flowinfo)-=-unpack_sockaddr_in6-$sockaddr"><a class="permalink" href="Socket.html#($port,-$ip6_address,-$scope_id,-$flowinfo)-=-unpack_sockaddr_in6-$sockaddr">#</a><a id="port-ip6_address-scope_id-flowinfo-unpack_sockaddr_in6-sockaddr"></a>($port, $ip6_address, $scope_id, $flowinfo) = unpack_sockaddr_in6 $sockaddr</h2>

<p>Takes a <code>sockaddr_in6</code> structure. Returns a list of four elements: the port number, an opaque string representing the IPv6 address, the scope ID, and the flow label. (You can use inet_ntop() to convert the address to the usual string format). Will croak if the structure does not represent an <code>AF_INET6</code> address.</p>

<p>In scalar context will return just the IP address.</p>

<h2 id="$sockaddr-=-sockaddr_in6-$port,-$ip6_address,-[$scope_id,-[$flowinfo]]"><a class="permalink" href="Socket.html#$sockaddr-=-sockaddr_in6-$port,-$ip6_address,-%5B$scope_id,-%5B$flowinfo%5D%5D">#</a><a id="sockaddr-sockaddr_in6-port-ip6_address-scope_id-flowinfo"></a>$sockaddr = sockaddr_in6 $port, $ip6_address, [$scope_id, [$flowinfo]]</h2>

<h2 id="($port,-$ip6_address,-$scope_id,-$flowinfo)-=-sockaddr_in6-$sockaddr"><a class="permalink" href="Socket.html#($port,-$ip6_address,-$scope_id,-$flowinfo)-=-sockaddr_in6-$sockaddr">#</a><a id="port-ip6_address-scope_id-flowinfo-sockaddr_in6-sockaddr"></a>($port, $ip6_address, $scope_id, $flowinfo) = sockaddr_in6 $sockaddr</h2>

<p>A wrapper of pack_sockaddr_in6() or unpack_sockaddr_in6(). In list context, unpacks its argument according to unpack_sockaddr_in6(). In scalar context, packs its arguments according to pack_sockaddr_in6().</p>

<p>Provided largely for legacy compatibility; it is better to use pack_sockaddr_in6() or unpack_sockaddr_in6() explicitly.</p>

<h2 id="$sockaddr-=-pack_sockaddr_un-$path"><a class="permalink" href="Socket.html#$sockaddr-=-pack_sockaddr_un-$path">#</a><a id="sockaddr-pack_sockaddr_un-path"></a>$sockaddr = pack_sockaddr_un $path</h2>

<p>Takes one argument, a pathname. Returns the <code>sockaddr_un</code> structure with that path packed in with <code>AF_UNIX</code> filled in. For <code>PF_UNIX</code> sockets, this structure is normally what you need for the arguments in bind(), connect(), and send().</p>

<h2 id="($path)-=-unpack_sockaddr_un-$sockaddr"><a class="permalink" href="Socket.html#($path)-=-unpack_sockaddr_un-$sockaddr">#</a><a id="path-unpack_sockaddr_un-sockaddr"></a>($path) = unpack_sockaddr_un $sockaddr</h2>

<p>Takes a <code>sockaddr_un</code> structure (as returned by pack_sockaddr_un(), getpeername() or recv()). Returns a list of one element: the pathname. Will croak if the structure does not represent an <code>AF_UNIX</code> address.</p>

<h2 id="$sockaddr-=-sockaddr_un-$path"><a class="permalink" href="Socket.html#$sockaddr-=-sockaddr_un-$path">#</a><a id="sockaddr-sockaddr_un-path"></a>$sockaddr = sockaddr_un $path</h2>

<h2 id="($path)-=-sockaddr_un-$sockaddr"><a class="permalink" href="Socket.html#($path)-=-sockaddr_un-$sockaddr">#</a><a id="path-sockaddr_un-sockaddr"></a>($path) = sockaddr_un $sockaddr</h2>

<p>A wrapper of pack_sockaddr_un() or unpack_sockaddr_un(). In a list context, unpacks its argument and returns a list consisting of the pathname. In a scalar context, packs its pathname as a <code>sockaddr_un</code> and returns it.</p>

<p>Provided largely for legacy compatibility; it is better to use pack_sockaddr_un() or unpack_sockaddr_un() explicitly.</p>

<p>These are only supported if your system has &lt;<i>sys/un.h</i>&gt;.</p>

<h2 id="$ip_mreq-=-pack_ip_mreq-$multiaddr,-$interface"><a class="permalink" href="Socket.html#$ip_mreq-=-pack_ip_mreq-$multiaddr,-$interface">#</a><a id="ip_mreq-pack_ip_mreq-multiaddr-interface"></a>$ip_mreq = pack_ip_mreq $multiaddr, $interface</h2>

<p>Takes an IPv4 multicast address and optionally an interface address (or <code>INADDR_ANY</code>). Returns the <code>ip_mreq</code> structure with those arguments packed in. Suitable for use with the <code>IP_ADD_MEMBERSHIP</code> and <code>IP_DROP_MEMBERSHIP</code> sockopts.</p>

<h2 id="($multiaddr,-$interface)-=-unpack_ip_mreq-$ip_mreq"><a class="permalink" href="Socket.html#($multiaddr,-$interface)-=-unpack_ip_mreq-$ip_mreq">#</a><a id="multiaddr-interface-unpack_ip_mreq-ip_mreq"></a>($multiaddr, $interface) = unpack_ip_mreq $ip_mreq</h2>

<p>Takes an <code>ip_mreq</code> structure. Returns a list of two elements; the IPv4 multicast address and interface address.</p>

<h2 id="$ip_mreq_source-=-pack_ip_mreq_source-$multiaddr,-$source,-$interface"><a class="permalink" href="Socket.html#$ip_mreq_source-=-pack_ip_mreq_source-$multiaddr,-$source,-$interface">#</a><a id="ip_mreq_source-pack_ip_mreq_source-multiaddr-source-interface"></a>$ip_mreq_source = pack_ip_mreq_source $multiaddr, $source, $interface</h2>

<p>Takes an IPv4 multicast address, source address, and optionally an interface address (or <code>INADDR_ANY</code>). Returns the <code>ip_mreq_source</code> structure with those arguments packed in. Suitable for use with the <code>IP_ADD_SOURCE_MEMBERSHIP</code> and <code>IP_DROP_SOURCE_MEMBERSHIP</code> sockopts.</p>

<h2 id="($multiaddr,-$source,-$interface)-=-unpack_ip_mreq_source-$ip_mreq"><a class="permalink" href="Socket.html#($multiaddr,-$source,-$interface)-=-unpack_ip_mreq_source-$ip_mreq">#</a><a id="multiaddr-source-interface-unpack_ip_mreq_source-ip_mreq"></a>($multiaddr, $source, $interface) = unpack_ip_mreq_source $ip_mreq</h2>

<p>Takes an <code>ip_mreq_source</code> structure. Returns a list of three elements; the IPv4 multicast address, source address and interface address.</p>

<h2 id="$ipv6_mreq-=-pack_ipv6_mreq-$multiaddr6,-$ifindex"><a class="permalink" href="Socket.html#$ipv6_mreq-=-pack_ipv6_mreq-$multiaddr6,-$ifindex">#</a><a id="ipv6_mreq-pack_ipv6_mreq-multiaddr6-ifindex"></a>$ipv6_mreq = pack_ipv6_mreq $multiaddr6, $ifindex</h2>

<p>Takes an IPv6 multicast address and an interface number. Returns the <code>ipv6_mreq</code> structure with those arguments packed in. Suitable for use with the <code>IPV6_ADD_MEMBERSHIP</code> and <code>IPV6_DROP_MEMBERSHIP</code> sockopts.</p>

<h2 id="($multiaddr6,-$ifindex)-=-unpack_ipv6_mreq-$ipv6_mreq"><a class="permalink" href="Socket.html#($multiaddr6,-$ifindex)-=-unpack_ipv6_mreq-$ipv6_mreq">#</a><a id="multiaddr6-ifindex-unpack_ipv6_mreq-ipv6_mreq"></a>($multiaddr6, $ifindex) = unpack_ipv6_mreq $ipv6_mreq</h2>

<p>Takes an <code>ipv6_mreq</code> structure. Returns a list of two elements; the IPv6 address and an interface number.</p>

<h1 id="FUNCTIONS"><a class="permalink" href="Socket.html#FUNCTIONS">#</a>FUNCTIONS</h1>

<h2 id="$ip_address-=-inet_aton-$string"><a class="permalink" href="Socket.html#$ip_address-=-inet_aton-$string">#</a><a id="ip_address-inet_aton-string"></a>$ip_address = inet_aton $string</h2>

<p>Takes a string giving the name of a host, or a textual representation of an IP address and translates that to an packed binary address structure suitable to pass to pack_sockaddr_in(). If passed a hostname that cannot be resolved, returns <code>undef</code>. For multi-homed hosts (hosts with more than one address), the first address found is returned.</p>

<p>For portability do not assume that the result of inet_aton() is 32 bits wide, in other words, that it would contain only the IPv4 address in network order.</p>

<p>This IPv4-only function is provided largely for legacy reasons. Newly-written code should use getaddrinfo() or inet_pton() instead for IPv6 support.</p>

<h2 id="$string-=-inet_ntoa-$ip_address"><a class="permalink" href="Socket.html#$string-=-inet_ntoa-$ip_address">#</a><a id="string-inet_ntoa-ip_address"></a>$string = inet_ntoa $ip_address</h2>

<p>Takes a packed binary address structure such as returned by unpack_sockaddr_in() (or a v-string representing the four octets of the IPv4 address in network order) and translates it into a string of the form <code>d.d.d.d</code> where the <code>d</code>s are numbers less than 256 (the normal human-readable four dotted number notation for Internet addresses).</p>

<p>This IPv4-only function is provided largely for legacy reasons. Newly-written code should use getnameinfo() or inet_ntop() instead for IPv6 support.</p>

<h2 id="$address-=-inet_pton-$family,-$string"><a class="permalink" href="Socket.html#$address-=-inet_pton-$family,-$string">#</a><a id="address-inet_pton-family-string"></a>$address = inet_pton $family, $string</h2>

<p>Takes an address family (such as <code>AF_INET</code> or <code>AF_INET6</code>) and a string containing a textual representation of an address in that family and translates that to an packed binary address structure.</p>

<p>See also getaddrinfo() for a more powerful and flexible function to look up socket addresses given hostnames or textual addresses.</p>

<h2 id="$string-=-inet_ntop-$family,-$address"><a class="permalink" href="Socket.html#$string-=-inet_ntop-$family,-$address">#</a><a id="string-inet_ntop-family-address"></a>$string = inet_ntop $family, $address</h2>

<p>Takes an address family and a packed binary address structure and translates it into a human-readable textual representation of the address; typically in <code>d.d.d.d</code> form for <code>AF_INET</code> or <code>hhhh:hhhh::hhhh</code> form for <code>AF_INET6</code>.</p>

<p>See also getnameinfo() for a more powerful and flexible function to turn socket addresses into human-readable textual representations.</p>

<h2 id="($err,-@result)-=-getaddrinfo-$host,-$service,-[$hints]"><a class="permalink" href="Socket.html#($err,-@result)-=-getaddrinfo-$host,-$service,-%5B$hints%5D">#</a><a id="err-result-getaddrinfo-host-service-hints"></a>($err, @result) = getaddrinfo $host, $service, [$hints]</h2>

<p>Given both a hostname and service name, this function attempts to resolve the host name into a list of network addresses, and the service name into a protocol and port number, and then returns a list of address structures suitable to connect() to it.</p>

<p>Given just a host name, this function attempts to resolve it to a list of network addresses, and then returns a list of address structures giving these addresses.</p>

<p>Given just a service name, this function attempts to resolve it to a protocol and port number, and then returns a list of address structures that represent it suitable to bind() to. This use should be combined with the <code>AI_PASSIVE</code> flag; see below.</p>

<p>Given neither name, it generates an error.</p>

<p>If present, $hints should be a reference to a hash, where the following keys are recognised:</p>

<dl>

<dt id="flags-=&gt;-INT"><a class="permalink" href="Socket.html#flags-=%3E-INT">#</a><a id="flags"></a><a id="flags-INT"></a>flags =&gt; INT</dt>
<dd>

<p>A bitfield containing <code>AI_*</code> constants; see below.</p>

</dd>
<dt id="family-=&gt;-INT"><a class="permalink" href="Socket.html#family-=%3E-INT">#</a><a id="family"></a><a id="family-INT"></a>family =&gt; INT</dt>
<dd>

<p>Restrict to only generating addresses in this address family</p>

</dd>
<dt id="socktype-=&gt;-INT"><a class="permalink" href="Socket.html#socktype-=%3E-INT">#</a><a id="socktype"></a><a id="socktype-INT"></a>socktype =&gt; INT</dt>
<dd>

<p>Restrict to only generating addresses of this socket type</p>

</dd>
<dt id="protocol-=&gt;-INT"><a class="permalink" href="Socket.html#protocol-=%3E-INT">#</a><a id="protocol"></a><a id="protocol-INT"></a>protocol =&gt; INT</dt>
<dd>

<p>Restrict to only generating addresses for this protocol</p>

</dd>
</dl>

<p>The return value will be a list; the first value being an error indication, followed by a list of address structures (if no error occurred).</p>

<p>The error value will be a dualvar; comparable to the <code>EAI_*</code> error constants, or printable as a human-readable error message string. If no error occurred it will be zero numerically and an empty string.</p>

<p>Each value in the results list will be a hash reference containing the following fields:</p>

<dl>

<dt id="family-=&gt;-INT1"><a class="permalink" href="Socket.html#family-=%3E-INT1">#</a><a id="family1"></a><a id="family-INT1"></a>family =&gt; INT</dt>
<dd>

<p>The address family (e.g. <code>AF_INET</code>)</p>

</dd>
<dt id="socktype-=&gt;-INT1"><a class="permalink" href="Socket.html#socktype-=%3E-INT1">#</a><a id="socktype1"></a><a id="socktype-INT1"></a>socktype =&gt; INT</dt>
<dd>

<p>The socket type (e.g. <code>SOCK_STREAM</code>)</p>

</dd>
<dt id="protocol-=&gt;-INT1"><a class="permalink" href="Socket.html#protocol-=%3E-INT1">#</a><a id="protocol1"></a><a id="protocol-INT1"></a>protocol =&gt; INT</dt>
<dd>

<p>The protocol (e.g. <code>IPPROTO_TCP</code>)</p>

</dd>
<dt id="addr-=&gt;-STRING"><a class="permalink" href="Socket.html#addr-=%3E-STRING">#</a><a id="addr"></a><a id="addr-STRING"></a>addr =&gt; STRING</dt>
<dd>

<p>The address in a packed string (such as would be returned by pack_sockaddr_in())</p>

</dd>
<dt id="canonname-=&gt;-STRING"><a class="permalink" href="Socket.html#canonname-=%3E-STRING">#</a><a id="canonname"></a><a id="canonname-STRING"></a>canonname =&gt; STRING</dt>
<dd>

<p>The canonical name for the host if the <code>AI_CANONNAME</code> flag was provided, or <code>undef</code> otherwise. This field will only be present on the first returned address.</p>

</dd>
</dl>

<p>The following flag constants are recognised in the $hints hash. Other flag constants may exist as provided by the OS.</p>

<dl>

<dt id="AI_PASSIVE"><a class="permalink" href="Socket.html#AI_PASSIVE">#</a>AI_PASSIVE</dt>
<dd>

<p>Indicates that this resolution is for a local bind() for a passive (i.e. listening) socket, rather than an active (i.e. connecting) socket.</p>

</dd>
<dt id="AI_CANONNAME"><a class="permalink" href="Socket.html#AI_CANONNAME">#</a>AI_CANONNAME</dt>
<dd>

<p>Indicates that the caller wishes the canonical hostname (<code>canonname</code>) field of the result to be filled in.</p>

</dd>
<dt id="AI_NUMERICHOST"><a class="permalink" href="Socket.html#AI_NUMERICHOST">#</a>AI_NUMERICHOST</dt>
<dd>

<p>Indicates that the caller will pass a numeric address, rather than a hostname, and that getaddrinfo() must not perform a resolve operation on this name. This flag will prevent a possibly-slow network lookup operation, and instead return an error if a hostname is passed.</p>

</dd>
</dl>

<h2 id="($err,-$hostname,-$servicename)-=-getnameinfo-$sockaddr,-[$flags,-[$xflags]]"><a class="permalink" href="Socket.html#($err,-$hostname,-$servicename)-=-getnameinfo-$sockaddr,-%5B$flags,-%5B$xflags%5D%5D">#</a><a id="err-hostname-servicename-getnameinfo-sockaddr-flags-xflags"></a>($err, $hostname, $servicename) = getnameinfo $sockaddr, [$flags, [$xflags]]</h2>

<p>Given a packed socket address (such as from getsockname(), getpeername(), or returned by getaddrinfo() in a <code>addr</code> field), returns the hostname and symbolic service name it represents. $flags may be a bitmask of <code>NI_*</code> constants, or defaults to 0 if unspecified.</p>

<p>The return value will be a list; the first value being an error condition, followed by the hostname and service name.</p>

<p>The error value will be a dualvar; comparable to the <code>EAI_*</code> error constants, or printable as a human-readable error message string. The host and service names will be plain strings.</p>

<p>The following flag constants are recognised as $flags. Other flag constants may exist as provided by the OS.</p>

<dl>

<dt id="NI_NUMERICHOST"><a class="permalink" href="Socket.html#NI_NUMERICHOST">#</a>NI_NUMERICHOST</dt>
<dd>

<p>Requests that a human-readable string representation of the numeric address be returned directly, rather than performing a name resolve operation that may convert it into a hostname. This will also avoid potentially-blocking network IO.</p>

</dd>
<dt id="NI_NUMERICSERV"><a class="permalink" href="Socket.html#NI_NUMERICSERV">#</a>NI_NUMERICSERV</dt>
<dd>

<p>Requests that the port number be returned directly as a number representation rather than performing a name resolve operation that may convert it into a service name.</p>

</dd>
<dt id="NI_NAMEREQD"><a class="permalink" href="Socket.html#NI_NAMEREQD">#</a>NI_NAMEREQD</dt>
<dd>

<p>If a name resolve operation fails to provide a name, then this flag will cause getnameinfo() to indicate an error, rather than returning the numeric representation as a human-readable string.</p>

</dd>
<dt id="NI_DGRAM"><a class="permalink" href="Socket.html#NI_DGRAM">#</a>NI_DGRAM</dt>
<dd>

<p>Indicates that the socket address relates to a <code>SOCK_DGRAM</code> socket, for the services whose name differs between TCP and UDP protocols.</p>

</dd>
</dl>

<p>The following constants may be supplied as $xflags.</p>

<dl>

<dt id="NIx_NOHOST"><a class="permalink" href="Socket.html#NIx_NOHOST">#</a>NIx_NOHOST</dt>
<dd>

<p>Indicates that the caller is not interested in the hostname of the result, so it does not have to be converted. <code>undef</code> will be returned as the hostname.</p>

</dd>
<dt id="NIx_NOSERV"><a class="permalink" href="Socket.html#NIx_NOSERV">#</a>NIx_NOSERV</dt>
<dd>

<p>Indicates that the caller is not interested in the service name of the result, so it does not have to be converted. <code>undef</code> will be returned as the service name.</p>

</dd>
</dl>

<h1 id="getaddrinfo()-/-getnameinfo()-ERROR-CONSTANTS"><a class="permalink" href="Socket.html#getaddrinfo()-/-getnameinfo()-ERROR-CONSTANTS">#</a><a id="getaddrinfo"></a><a id="getaddrinfo-getnameinfo-ERROR-CONSTANTS"></a>getaddrinfo() / getnameinfo() ERROR CONSTANTS</h1>

<p>The following constants may be returned by getaddrinfo() or getnameinfo(). Others may be provided by the OS.</p>

<dl>

<dt id="EAI_AGAIN"><a class="permalink" href="Socket.html#EAI_AGAIN">#</a>EAI_AGAIN</dt>
<dd>

<p>A temporary failure occurred during name resolution. The operation may be successful if it is retried later.</p>

</dd>
<dt id="EAI_BADFLAGS"><a class="permalink" href="Socket.html#EAI_BADFLAGS">#</a>EAI_BADFLAGS</dt>
<dd>

<p>The value of the <code>flags</code> hint to getaddrinfo(), or the $flags parameter to getnameinfo() contains unrecognised flags.</p>

</dd>
<dt id="EAI_FAMILY"><a class="permalink" href="Socket.html#EAI_FAMILY">#</a>EAI_FAMILY</dt>
<dd>

<p>The <code>family</code> hint to getaddrinfo(), or the family of the socket address passed to getnameinfo() is not supported.</p>

</dd>
<dt id="EAI_NODATA"><a class="permalink" href="Socket.html#EAI_NODATA">#</a>EAI_NODATA</dt>
<dd>

<p>The host name supplied to getaddrinfo() did not provide any usable address data.</p>

</dd>
<dt id="EAI_NONAME"><a class="permalink" href="Socket.html#EAI_NONAME">#</a>EAI_NONAME</dt>
<dd>

<p>The host name supplied to getaddrinfo() does not exist, or the address supplied to getnameinfo() is not associated with a host name and the <code>NI_NAMEREQD</code> flag was supplied.</p>

</dd>
<dt id="EAI_SERVICE"><a class="permalink" href="Socket.html#EAI_SERVICE">#</a>EAI_SERVICE</dt>
<dd>

<p>The service name supplied to getaddrinfo() is not available for the socket type given in the $hints.</p>

</dd>
</dl>

<h1 id="EXAMPLES"><a class="permalink" href="Socket.html#EXAMPLES">#</a>EXAMPLES</h1>

<h2 id="Lookup-for-connect()"><a class="permalink" href="Socket.html#Lookup-for-connect()">#</a><a id="Lookup"></a><a id="Lookup-for-connect"></a>Lookup for connect()</h2>

<p>The getaddrinfo() function converts a hostname and a service name into a list of structures, each containing a potential way to connect() to the named service on the named host.</p>

<pre><code>use IO::Socket;
use Socket qw(SOCK_STREAM getaddrinfo);

my %hints = (socktype =&gt; SOCK_STREAM);
my ($err, @res) = getaddrinfo(&quot;localhost&quot;, &quot;echo&quot;, \%hints);
die &quot;Cannot getaddrinfo - $err&quot; if $err;

my $sock;

foreach my $ai (@res) {
    my $candidate = IO::Socket-&gt;new();

    $candidate-&gt;socket($ai-&gt;{family}, $ai-&gt;{socktype}, $ai-&gt;{protocol})
        or next;

    $candidate-&gt;connect($ai-&gt;{addr})
        or next;

    $sock = $candidate;
    last;
}

die &quot;Cannot connect to localhost:echo&quot; unless $sock;

$sock-&gt;print(&quot;Hello, world!\n&quot;);
print &lt;$sock&gt;;</code></pre>

<p>Because a list of potential candidates is returned, the <code>while</code> loop tries each in turn until it finds one that succeeds both the socket() and connect() calls.</p>

<p>This function performs the work of the legacy functions gethostbyname(), getservbyname(), inet_aton() and pack_sockaddr_in().</p>

<p>In practice this logic is better performed by <a href="./IO::Socket::IP.html">IO::Socket::IP</a>.</p>

<h2 id="Making-a-human-readable-string-out-of-an-address"><a class="permalink" href="Socket.html#Making-a-human-readable-string-out-of-an-address">#</a><a id="Making"></a>Making a human-readable string out of an address</h2>

<p>The getnameinfo() function converts a socket address, such as returned by getsockname() or getpeername(), into a pair of human-readable strings representing the address and service name.</p>

<pre><code>use IO::Socket::IP;
use Socket qw(getnameinfo);

my $server = IO::Socket::IP-&gt;new(LocalPort =&gt; 12345, Listen =&gt; 1) or
    die &quot;Cannot listen - $@&quot;;

my $socket = $server-&gt;accept or die &quot;accept: $!&quot;;

my ($err, $hostname, $servicename) = getnameinfo($socket-&gt;peername);
die &quot;Cannot getnameinfo - $err&quot; if $err;

print &quot;The peer is connected from $hostname\n&quot;;</code></pre>

<p>Since in this example only the hostname was used, the redundant conversion of the port number into a service name may be omitted by passing the <code>NIx_NOSERV</code> flag.</p>

<pre><code>use Socket qw(getnameinfo NIx_NOSERV);

my ($err, $hostname) = getnameinfo($socket-&gt;peername, 0, NIx_NOSERV);</code></pre>

<p>This function performs the work of the legacy functions unpack_sockaddr_in(), inet_ntoa(), gethostbyaddr() and getservbyport().</p>

<p>In practice this logic is better performed by <a href="./IO::Socket::IP.html">IO::Socket::IP</a>.</p>

<h2 id="Resolving-hostnames-into-IP-addresses"><a class="permalink" href="Socket.html#Resolving-hostnames-into-IP-addresses">#</a><a id="Resolving"></a>Resolving hostnames into IP addresses</h2>

<p>To turn a hostname into a human-readable plain IP address use getaddrinfo() to turn the hostname into a list of socket structures, then getnameinfo() on each one to make it a readable IP address again.</p>

<pre><code>use Socket qw(:addrinfo SOCK_RAW);

my ($err, @res) = getaddrinfo($hostname, &quot;&quot;, {socktype =&gt; SOCK_RAW});
die &quot;Cannot getaddrinfo - $err&quot; if $err;

while( my $ai = shift @res ) {
    my ($err, $ipaddr) = getnameinfo($ai-&gt;{addr}, NI_NUMERICHOST, NIx_NOSERV);
    die &quot;Cannot getnameinfo - $err&quot; if $err;

    print &quot;$ipaddr\n&quot;;
}</code></pre>

<p>The <code>socktype</code> hint to getaddrinfo() filters the results to only include one socket type and protocol. Without this most OSes return three combinations, for <code>SOCK_STREAM</code>, <code>SOCK_DGRAM</code> and <code>SOCK_RAW</code>, resulting in triplicate output of addresses. The <code>NI_NUMERICHOST</code> flag to getnameinfo() causes it to return a string-formatted plain IP address, rather than reverse resolving it back into a hostname.</p>

<p>This combination performs the work of the legacy functions gethostbyname() and inet_ntoa().</p>

<h2 id="Accessing-socket-options"><a class="permalink" href="Socket.html#Accessing-socket-options">#</a><a id="Accessing"></a>Accessing socket options</h2>

<p>The many <code>SO_*</code> and other constants provide the socket option names for getsockopt() and setsockopt().</p>

<pre><code>use IO::Socket::INET;
use Socket qw(SOL_SOCKET SO_RCVBUF IPPROTO_IP IP_TTL);

my $socket = IO::Socket::INET-&gt;new(LocalPort =&gt; 0, Proto =&gt; &#39;udp&#39;)
    or die &quot;Cannot create socket: $@&quot;;

$socket-&gt;setsockopt(SOL_SOCKET, SO_RCVBUF, 64*1024) or
    die &quot;setsockopt: $!&quot;;

print &quot;Receive buffer is &quot;, $socket-&gt;getsockopt(SOL_SOCKET, SO_RCVBUF),
    &quot; bytes\n&quot;;

print &quot;IP TTL is &quot;, $socket-&gt;getsockopt(IPPROTO_IP, IP_TTL), &quot;\n&quot;;</code></pre>

<p>As a convenience, <a href="./IO::Socket.html">IO::Socket</a>&#39;s setsockopt() method will convert a number into a packed byte buffer, and getsockopt() will unpack a byte buffer of the correct size back into a number.</p>

<h1 id="AUTHOR"><a class="permalink" href="Socket.html#AUTHOR">#</a>AUTHOR</h1>

<p>This module was originally maintained in Perl core by the Perl 5 Porters.</p>

<p>It was extracted to dual-life on CPAN at version 1.95 by Paul Evans &lt;leonerd@leonerd.org.uk&gt;</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
